package com.test.test01.config.rabbitmq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * Description: RabbitMQ 配置
 * Create DateTime: 2020-03-28 18:52
 *
 * @author houyi
 */
@Slf4j
@Configuration
public class RabbitMQConfig {

    //===================================direct==========================================

    /**
     * 声明direct交换机  --点对点
     */
    @Bean
    public Exchange directExchange() {
        return new DirectExchange(RabbitMQConstant.RABBIT_DIRECT_EXCHANGE, true, false);
    }


    /**
     * 声明direct队列
     */
    @Bean
    public Queue directQueueA() {
        return new Queue(RabbitMQConstant.RABBIT_DIRECT_QUEUEA, true);
    }

    @Bean
    public Queue directQueueB() {
        return new Queue(RabbitMQConstant.RABBIT_DIRECT_QUEUEB, true);
    }


    /**
     * 绑定direct队列到direct交换机
     */
    @Bean
    public Binding directBindingA() {
        return BindingBuilder.bind(directQueueA()).to(directExchange()).with(RabbitMQConstant.RABBIT_DIRECT_ROUTING_KEY).noargs();
    }

    @Bean
    public Binding directBindingB() {
        return BindingBuilder.bind(directQueueB()).to(directExchange()).with(RabbitMQConstant.RABBIT_DIRECT_ROUTING_KEY).noargs();
    }


    //=====================================topic====================================================

    /**
     * 声明topic交换机
     */
    @Bean
    public Exchange topicExchange() {
        return new TopicExchange(RabbitMQConstant.RABBIT_TOPIC_EXCHANGE, true, false);
    }


    /**
     * 声明topic队列
     */
    @Bean
    public Queue topicQueueA() {
        return new Queue(RabbitMQConstant.RABBIT_TOPIC_A_QUEUE, true);
    }

    @Bean
    public Queue topicQueueB() {
        return new Queue(RabbitMQConstant.RABBIT_TOPIC_B_QUEUE, true);
    }

    /**
     * 绑定交换机和队列
     */
    @Bean
    public Binding topicBindingA() {
        return BindingBuilder.bind(topicQueueA()).to(topicExchange()).with(RabbitMQConstant.RABBIT_TOPIC_ROUTING_A_KEY).noargs();
    }

    @Bean
    public Binding topicBindingB() {
        return BindingBuilder.bind(topicQueueB()).to(topicExchange()).with(RabbitMQConstant.RABBIT_TOPIC_ROUTING_ALL_KEY).noargs();
    }

    //=====================================fanout====================================================

    /**
     * 声明fanout交换机
     */
    @Bean
    public FanoutExchange fanoutExchange() {
        return new FanoutExchange(RabbitMQConstant.RABBIT_FANOUT_EXCHANGE, true, false);
    }


    /**
     * 声明fanout队列
     */
    @Bean
    public Queue fanoutQueueA() {
        return new Queue(RabbitMQConstant.RABBIT_FANOUT_QUEUEA, true);
    }

    @Bean
    public Queue fanoutQueueB() {
        return new Queue(RabbitMQConstant.RABBIT_FANOUT_QUEUEB, true);
    }

    @Bean
    public Queue fanoutQueueC() {
        return new Queue(RabbitMQConstant.RABBIT_FANOUT_QUEUEC, true);
    }

    /**
     * 绑定交换机和队列
     */
    @Bean
    public Binding fanoutBindingA() {
        return BindingBuilder.bind(fanoutQueueA()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBindingB() {
        return BindingBuilder.bind(fanoutQueueB()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBindingC() {
        return BindingBuilder.bind(fanoutQueueC()).to(fanoutExchange());
    }


    //=====================================Delayed====================================================

    /**
     * 声明延时交换机
     * 注:需要在rabbitmq中安装配置 rabbitmq_delayed_message_exchange 插件
     */
    @Bean
    public CustomExchange delayExchange() {
        Map<String, Object> args = new HashMap<>(2);
        args.put("x-delayed-type", "direct");
        return new CustomExchange(RabbitMQConstant.RABBIT_DELAY_EXCHANGE,
                RabbitMQConstant.RABBIT_DELAY_EXCHANGE_TYPE, true, false, args);

    }

    /**
     * 声明延时队列
     */
    @Bean
    public Queue delayExpireQueue() {
        return new Queue(RabbitMQConstant.RABBIT_DELAY_EXPIRED_QUEUE, true);
    }

    /**
     * 绑定delay订单支付超时队列到delay交换机
     */
    @Bean
    public Binding delayExpireBinding() {
        return BindingBuilder.bind(delayExpireQueue()).to(delayExchange()).with(RabbitMQConstant.RABBIT_DELAY_EXPIRED_ROUTING_KEY).noargs();
    }
}
